//
//  TouchMoveScale.cpp
//  cake
//
//  Created by luotianqiang1 on 17/2/28.
//
//

#include "TouchMoveScale.h"
IMPLEMENT_COCOS2DX_CLASS(TouchMoveScale);

TouchMoveScale::TouchMoveScale(){
    touchPoint = Vec2::ZERO;
    length = 0;
    maxScale = 1.2;
    minScale = 0.7;
}

bool TouchMoveScale::init(){
    if(TouchMoveComponent::init()){
        return true;
    }
    return false;
}

bool TouchMoveScale::touchBeganLis(Touch* pTouch,Event*){
    auto touchIndex = pTouch->getID();
    if(touchIndex!=0&&_touches.size()==0)
        return false;
    
    auto pos = getOwner()->convertToNodeSpace(pTouch->getLocation());
    if(!isCotain(pTouch)) {
        if(_touches.size()==0)
            return false;
    }
    if(_touches.size()>=2)
        return true;
    if(std::find(_touches.begin(), _touches.end(), pTouch) == _touches.end())
        _touches.push_back(pTouch);
    if(_touches.size() ==1) {
        touchPoint = pTouch->getLocation();
    }
    else if(_touches.size() == 2) {
        touchPoint = _touches[0]->getLocation().getMidpoint(_touches[1]->getLocation());
        length = _touches[0]->getLocation().distance(_touches[1]->getLocation());
    }
    return true;
}
void TouchMoveScale::touchMoveLis(Touch* pTouch,Event*){
    if(std::find(_touches.begin(), _touches.end(), pTouch) != _touches.end()) {
        if(_touches.size() == 1) {
            auto newPoint = pTouch->getLocation();
            auto disance = newPoint-touchPoint;
            touchPoint = newPoint;
             move(disance);
            
        }else if(_touches.size() == 2) {
            
            auto newPoint = _touches[0]->getLocation().getMidpoint(_touches[1]->getLocation());
            auto disance = newPoint-touchPoint;
            touchPoint = newPoint;
            
            auto curentLen =  _touches[0]->getLocation().distance(_touches[1]->getLocation());
            auto distanceLen = curentLen-length;
            length = curentLen;
            
            
             scale(distanceLen);
        }
    }
}
void TouchMoveScale::touchEndLis(Touch* pTouch,Event*){
    auto it = std::find(_touches.begin(), _touches.end(), pTouch);
    if(it != _touches.end()) {
        _touches.erase(it);
        if(_touches.size() == 1)
            touchPoint = _touches[0]->getLocation();
        
    }
}
void TouchMoveScale::touchCanceLis(Touch* pTouch,Event*){
    auto it = std::find(_touches.begin(), _touches.end(), pTouch);
    if(it != _touches.end()) {
        _touches.erase(it);
        if(_touches.size() == 1)
            touchPoint = _touches[0]->getLocation();
    }
}

void TouchMoveScale::scale(float distace) {
    auto touchPoint = this->touchPoint;
//    if(centerPoint.size()>0)
//        touchPoint = centerPoint.at(0);
    
    auto scale = distace/Vec2(_owner->getContentSize()).getLength();
    auto newscale = scale+_owner->getScale();
    
//    
    if(newscale>maxScale||newscale<minScale)
        return;
//   auto orns = _owner->getScale();
    _owner->setScale(newscale);
//    if(_limit != nullptr) {
//        for(const auto & t:_polygon->getPolygonPoits()){
//            auto v = convertToWorldSpace(t);
//            if(!PolygonHelp::isPointInPolygon(v,_limit->getPolygonPoits())){
//                setScale(orns);
//                break;
//            }
//        }
//    }
}

void TouchMoveScale::move(Vec2 distance) {
    //    auto p = convertToWorldSpace(getContentSize()*.5)+distance;
    //    bool isContain = true;
    //    if(_limit != nullptr) {
    //        for(const auto & t:_polygon->getPolygonPoits()){
    //            auto v = convertToWorldSpace(t)+distance;
    //            if(!PolygonHelp::isPointInPolygon(v,_limit->getPolygonPoits())){
    //                isContain = false;
    //                break;
    //            }
    //        }
    //    }
    //    if(isContain)
    _owner->setPosition(_owner->getPosition()+distance);
}